/**
 * 
 */
package cn.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import cn.crm.bean.Right;
import cn.crm.bean.RightMaping;
import cn.crm.bean.User;
import cn.crm.dao.RightMapingDao;
import cn.crm.service.RightMapingService;
import cn.crm.service.RightService;
import cn.crm.service.UserService;

/**
 * @author Vi1___000
 *
 */
@Service("rightMapingService")
public class RightMapingServiceImpl implements RightMapingService {
	
	@Resource
	private RightMapingDao rightMapingDao;

	/* (non-Javadoc)
	 * @see cn.crm.service.RightMapingService#getJSONByUser(cn.crm.bean.User)
	 */
	@Override
	public String getJSONByUser(User user) {
		// TODO Auto-generated method stub
		JSONArray jlist  = new JSONArray();
		List<RightMaping> list = rightMapingDao.getByUser(user);
		for (RightMaping rp : list) {
			JSONObject obj = new JSONObject();
			obj.put("id", rp.getMid());
			obj.put("name", rp.getRight().getRname());
			obj.put("rurl", rp.getRight().getRurl());
			obj.put("rid", rp.getRight().getRid());
			obj.put("parentR", rp.getRight().getRight().getRname());
			obj.put("prid", rp.getRight().getParentid());
			obj.put("noMenu", rp.getRight().getZtype());
			jlist.add(obj);
		}
		
		return jlist.toJSONString();
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightMapingService#getByUser(cn.crm.bean.User)
	 */
	@Override
	public List<RightMaping> getByUser(User user) {
		// TODO Auto-generated method stub
		return rightMapingDao.getByUser(user);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightMapingService#add(cn.crm.bean.RightMaping)
	 */
	@Override
	public boolean add(RightMaping maping) {
		// TODO Auto-generated method stub
		maping.setIsok(true);
		return rightMapingDao.add(maping);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightMapingService#del(cn.crm.bean.RightMaping)
	 */
	@Override
	public boolean del(RightMaping maping) {
		// TODO Auto-generated method stub
		return rightMapingDao.del(maping);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightMapingService#update(cn.crm.bean.RightMaping)
	 */
	@Override
	public boolean remove(RightMaping maping) {
			return rightMapingDao.delByUserRight(maping.getUser().getUsCid(), maping.getRight().getRid());
	}

	public void setRightMapingDao(RightMapingDao rightMapingDao) {
		this.rightMapingDao = rightMapingDao;
	}

	@Override
	public RightMaping getByID(Integer id) {
		// TODO Auto-generated method stub
		return rightMapingDao.getByID(id);
	}

	
	

}
